﻿using MathTutorPro.DAL;
using MathTutorPro.Data.Entities;
using MathTutorPro.Models;
using MathTutorPro.Models.Requests;
using MathTutorPro.Services.Interfaces;
using MathTutorPro.Utilities;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.ComponentModel.DataAnnotations;
using System.Security.Claims;

namespace MathTutorPro.Controllers
{
    [ApiController]
    [Route("api/class/{classId}/[controller]")]
    //[Authorize(Roles = "Teacher")]
    public class ClassDetailController : ControllerBase
    {
        private readonly IClassDetailService _classDetailService;
        private readonly ILogger<ClassDetailController> _logger;

        public ClassDetailController(
        IClassDetailService classDetailService,
        ILogger<ClassDetailController> logger)
        {
            _classDetailService = classDetailService;
            _logger = logger;
        }


        // GET: api/class/{classId}/classdetail
        //获取指定班级的成员详细信息
        [HttpGet]
        public async Task<IActionResult> GetClassDetail(int classId, [FromQuery] string keyword = null)
        {
            var teacherId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var students = await _classDetailService.GetClassStudentsAsync(classId, teacherId, keyword);
            return Ok(new { code = 20000, data = students });
        }

        // POST: api/classes/{classId}/students
        // 添加班级明细（成员）
        [HttpPost]
        public async Task<IActionResult> CreateStudent(
        int classId,
        [FromBody] StudentCreateRequest dto)
        {
            var teacherId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var studentId = await _classDetailService.AddStudentAsync(classId, teacherId, dto);
            return CreatedAtAction(
                nameof(GetStudent),
                new { classId, studentId },
                new { code = 20000, data = studentId });
        }

        [HttpGet("{studentId}")]
        public async Task<IActionResult> GetStudent(int classId, string studentId)
        {
            var teacherId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            var student = await _classDetailService.GetStudentAsync(classId, teacherId, studentId);
            return Ok(new { code = 20000, data = student });
        }


        // PUT: api/class/{classId}/ClassDetail/{userId}
        [HttpPut("{userId}")]
        public async Task<IActionResult> UpdateStudent(
        int classId,
        string userId,
        [FromBody] StudentUpdateRequest dto)
        {
            var teacherId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            await _classDetailService.UpdateStudentAsync(classId, teacherId, userId, dto);
            return Ok(new { code = 20000, message = "更新成功" });
        }

        [HttpPost("import")]
        public async Task<IActionResult> ImportStudents(
       int classId,
       [FromForm] IFormFile file)
        {
            var teacherId = User.FindFirstValue(ClaimTypes.NameIdentifier);
            await _classDetailService.ImportStudentsAsync(classId, teacherId, file);
            return Ok(new { code = 20000, message = "导入成功" });
        }





    }
}
